package com.cskaoyan.service.configmanage;

import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.module.bean.BaseRespVo;

import com.cskaoyan.module.bean.configmanageBo.AdminConfigMall4PostBO;
import com.cskaoyan.module.bean.configmanageBo.AdminConfigOrderBo;
import com.cskaoyan.module.bean.configmanageBo.AdminConfigWxBo;
import com.cskaoyan.module.bean.configmanageBo.ShippingRateConfigurationBO;
import com.cskaoyan.module.pojo.MarketSystem;
import com.cskaoyan.module.pojo.MarketSystemExample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.sql.Date;
import java.util.*;

@Service
public class MarketConfigServiceImpl implements MarketConfigService {
    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public BaseRespVo getInitInformation() {
//id={16,3,13,7,14,9}
        Integer[] integers = new Integer[]{16, 3, 13, 7, 14, 9};
        List<MarketSystem> marketSystems = getMarketSystemsByIds(integers);
        HashMap<String, String> firstInformation = new HashMap<>();

        for (MarketSystem marketSystem : marketSystems) {
            firstInformation.put(marketSystem.getKeyName(), marketSystem.getKeyValue());

        }


        return BaseRespVo.ok(firstInformation);
    }

    private List<MarketSystem> getMarketSystemsByIds(Integer[] integers) {
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();

        criteria.andIdIn(Arrays.asList(integers));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(example);
        return marketSystems;
    }

    @Override
    @Transactional
    public BaseRespVo upDataInfomation(AdminConfigMall4PostBO adminConfigMall4PostBO) {
        String marketMallQq = adminConfigMall4PostBO.getMarketMallQq();
        String marketMallPhone = adminConfigMall4PostBO.getMarketMallPhone();
        String marketMallName = adminConfigMall4PostBO.getMarketMallName();
        String marketMallAddress = adminConfigMall4PostBO.getMarketMallAddress();
        String marketMallLatitude = adminConfigMall4PostBO.getMarketMallLatitude();
        String marketMallLongitude = adminConfigMall4PostBO.getMarketMallLongitude();




        HashMap<String, String> getKeyNameHashMap = new HashMap<>();
        getKeyNameHashMap.put(marketMallQq, "market_mall_qq");
        getKeyNameHashMap.put(marketMallPhone, "market_mall_phone");
        getKeyNameHashMap.put(marketMallName, "market_mall_name");
        getKeyNameHashMap.put(marketMallAddress, "market_mall_address");
        getKeyNameHashMap.put(marketMallLatitude, "market_mall_latitude");
        getKeyNameHashMap.put(marketMallLongitude, "market_mall_longitude");
        ArrayList<String> keyValues = new ArrayList<>();
        keyValues.add(marketMallQq);
        keyValues.add(marketMallPhone);
        keyValues.add(marketMallName);
        keyValues.add(marketMallAddress);
        keyValues.add(marketMallLatitude);
        keyValues.add(marketMallLongitude);
        int update = 0;//标记插入几条
        for (String keyValue : keyValues) {
//            MarketSystem record = new MarketSystem();
//            record.setKeyValue(keyValue);
//            record.setKeyName(getKeyNameHashMap.get(keyValue));
//            record.setDeleted(false);
//            record.setAddTime(new Date(System.currentTimeMillis()));
//            insert += marketSystemMapper.insert(record);
            MarketSystem record = new MarketSystem();
            record.setKeyValue(keyValue);
            record.setUpdateTime(new Date(System.currentTimeMillis()));
            update += getUpdateByKeyValueName(getKeyNameHashMap.get(keyValue), record);
        }
        if (update < 4) {
            try {
                throw new Exception("插入信息异常");
            } catch (Exception e) {
                return BaseRespVo.invalidData(e.getMessage());
            }
        }


        return BaseRespVo.ok(null);
    }

    private int getUpdateByKeyValueName( String keyValueName, MarketSystem record) {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameEqualTo(keyValueName).andDeletedEqualTo(false);
        return  marketSystemMapper.updateByExampleSelective(record, marketSystemExample);

    }

    @Override
    @Transactional
    public BaseRespVo upShippingRate(ShippingRateConfigurationBO shippingRateConfigurationBO) {
        int insert = 0;//标记插入几条
        String marketExpressFreightMin = shippingRateConfigurationBO.getMarketExpressFreightMin();
        String marketExpressFreightValue = shippingRateConfigurationBO.getMarketExpressFreightValue();
        ArrayList<String> keyValues = new ArrayList<>();
        keyValues.add(marketExpressFreightMin);
        keyValues.add(marketExpressFreightValue);
        HashMap<String, String> getKeyNameHashMap = new HashMap<>();
        getKeyNameHashMap.put(marketExpressFreightMin, "market_express_freight_min");
        getKeyNameHashMap.put(marketExpressFreightValue, "market_express_freight_value");

        for (String keyValue : keyValues) {
            MarketSystem record = new MarketSystem();
            record.setKeyValue(keyValue);
//            record.setKeyName(getKeyNameHashMap.get(keyValue));

            insert += getUpdateByKeyValueName(getKeyNameHashMap.get(keyValue),record);
        }
        if (insert != keyValues.size()) {
            try {
                throw new Exception("插入信息异常");
            } catch (Exception e) {
                return BaseRespVo.invalidData(e.getMessage());
            }


        }
        return BaseRespVo.ok("成功");
    }

    @Override
    @Transactional
    public BaseRespVo upOrder(AdminConfigOrderBo adminConfigOrderBo) {
        String marketOrderComment = adminConfigOrderBo.getMarketOrderComment();
        String marketOrderUnconfirm = adminConfigOrderBo.getMarketOrderUnconfirm();
        String marketOrderUnpaid = adminConfigOrderBo.getMarketOrderUnpaid();
        HashMap<String, String> getKeyNameHashMap = new HashMap<>();
        ArrayList<String> keyValues = new ArrayList<>();
        keyValues.add(marketOrderComment);
        keyValues.add(marketOrderUnconfirm);
        keyValues.add(marketOrderUnpaid);
        getKeyNameHashMap.put(marketOrderComment, "market_order_comment");
        getKeyNameHashMap.put(marketOrderUnconfirm, "market_order_unconfirm");
        getKeyNameHashMap.put(marketOrderUnpaid, "market_order_unpaid");
        int insert = 0;//标记插入几条
        for (String keyValue : keyValues) {
            MarketSystem record = new MarketSystem();
            record.setKeyValue(keyValue);

            insert += getUpdateByKeyValueName(getKeyNameHashMap.get(keyValue),record);
        }
        if (insert != keyValues.size()) {
            try {
                throw new Exception("插入信息异常");
            } catch (Exception e) {
                return BaseRespVo.invalidData(e.getMessage());
            }


        }
        return BaseRespVo.ok("成功");
    }

    @Override
    @Transactional
    public BaseRespVo upWx(AdminConfigWxBo adminConfigWxBo) {

        HashMap<String, String> getKeyNameHashMap = new HashMap<>();
        ArrayList<String> keyValues = new ArrayList<>();
        int insert = 0;//标记插入几条
        String marketWxCatlogGoods = adminConfigWxBo.getMarketWxCatlogGoods();
        String marketWxCatlogList = adminConfigWxBo.getMarketWxCatlogList();
        String marketWxIndexBrand = adminConfigWxBo.getMarketWxIndexBrand();
        String marketWxIndexHot = adminConfigWxBo.getMarketWxIndexHot();
        String marketWxIndexNew = adminConfigWxBo.getMarketWxIndexNew();
        String marketWxIndexTopic = adminConfigWxBo.getMarketWxIndexTopic();
        String marketWxShare = adminConfigWxBo.getMarketWxShare();
        keyValues.add(marketWxCatlogGoods);
        keyValues.add(marketWxCatlogList);
        keyValues.add(marketWxIndexBrand);
        keyValues.add(marketWxIndexHot);
        keyValues.add(marketWxIndexNew);
        keyValues.add(marketWxIndexTopic);
        keyValues.add(marketWxShare);
        getKeyNameHashMap.put(marketWxCatlogGoods, "market_wx_catlog_goods");
        getKeyNameHashMap.put(marketWxCatlogList, "market_wx_catlog_list");
        getKeyNameHashMap.put(marketWxIndexBrand, "market_wx_index_brand");
        getKeyNameHashMap.put(marketWxIndexHot, "market_wx_index_hot");
        getKeyNameHashMap.put(marketWxIndexNew, "market_wx_index_new");
        getKeyNameHashMap.put(marketWxIndexTopic, "market_wx_index_topic");
        getKeyNameHashMap.put(marketWxShare, "market_wx_share");
        for (String keyValue : keyValues) {
            MarketSystem record = new MarketSystem();
            record.setKeyValue(keyValue);

            insert += getUpdateByKeyValueName(getKeyNameHashMap.get(keyValue),record);
        }
        if (insert != keyValues.size()) {
            try {
                throw new Exception("插入信息异常");
            } catch (Exception e) {
                return BaseRespVo.invalidData(e.getMessage());
            }


        }
        return BaseRespVo.ok("成功");
    }

    @Override
    public BaseRespVo getRate(Integer[] ids) {
        List<MarketSystem> marketSystemsByIds = getMarketSystemsByIds(ids);
        HashMap<String, String> getRate = new HashMap<>();
        for (MarketSystem marketSystemsById : marketSystemsByIds) {
            getRate.put(marketSystemsById.getKeyName(),marketSystemsById.getKeyValue());
        }
        return BaseRespVo.ok(getRate);
    }

    @Override
    public BaseRespVo getOrderInformation(Integer[] ids) {
        List<MarketSystem> marketSystemsByIds = getMarketSystemsByIds(ids);
        HashMap<String, String> getRate = new HashMap<>();
        for (MarketSystem marketSystemsById : marketSystemsByIds) {
            getRate.put(marketSystemsById.getKeyName(),marketSystemsById.getKeyValue());
        }
        return BaseRespVo.ok(getRate);

    }

    @Override
    public BaseRespVo getWxInformation(Integer[] ids) {
        List<MarketSystem> marketSystemsByIds = getMarketSystemsByIds(ids);
        HashMap<String, String> getRate = new HashMap<>();
        for (MarketSystem marketSystemsById : marketSystemsByIds) {
            getRate.put(marketSystemsById.getKeyName(),marketSystemsById.getKeyValue());
        }
        return BaseRespVo.ok(getRate);
    }
}
